import streamlit as st
from database_manager import DatabaseManager
from rag_processor import RAGProcessor
import json
import pandas as pd

def render_student_page():
    st.title("智能课程推荐系统")
    
    # 初始化组件
    if 'db_manager' not in st.session_state:
        st.session_state.db_manager = DatabaseManager()
    if 'rag_processor' not in st.session_state:
        st.session_state.rag_processor = RAGProcessor()
    if 'chat_history' not in st.session_state:
        st.session_state.chat_history = []
    
    # 聊天历史展示
    st.header("对话历史")
    for message in st.session_state.chat_history[-10:]:  # 只显示最近10条记录
        with st.chat_message(message["role"]):
            st.write(message["content"])
            if "recommendation" in message:
                st.json(message["recommendation"])
    
    # 用户输入
    user_input = st.chat_input("请输入你的需求，例如：我想学习Python编程")
    if user_input:
        # 添加用户消息到历史
        st.session_state.chat_history.append({
            "role": "user",
            "content": user_input
        })
        
        # 处理用户查询
        with st.chat_message("assistant"):
            with st.spinner("正在为您推荐课程..."):
                result = st.session_state.rag_processor.process_query(
                    user_input,
                    st.session_state.db_manager
                )
                
                # 构造推荐结果
                recommendation = result["recommendation"]
                similar_courses = result["similar_courses"]

                # 显示相似课程列表  
                st.write("相似课程列表：")
                if similar_courses:
                    # 准备表格数据
                    table_data = []
                    for course in similar_courses:
                        table_data.append({
                            "课程名称": course.get('name', '未知课程'),
                            "课程描述": course.get('description', '暂无描述'),
                            "适用年级": course.get('grade_level', '未知'),
                            "学科分类": course.get('subject', '未知'),
                            "难度等级": course.get('difficulty', '未知'),
                            "课程链接": course.get('course_url', '')
                        })
                    
                    # 使用pandas DataFrame创建表格
                    df = pd.DataFrame(table_data)
                    st.dataframe(
                        df,
                        use_container_width=True,
                        hide_index=True,
                        column_config={
                            "课程链接": st.column_config.LinkColumn(
                                "课程链接",
                                help="点击访问课程",
                                width="small"
                            )
                        }
                    )
                else:
                    st.info("未找到相似课程")
                
                # 显示推荐课程
                st.write("为您推荐的课程：")
                st.write(recommendation)
                
                # 添加助手回复到历史
                st.session_state.chat_history.append({
                    "role": "assistant",
                    "content": "已为您推荐相关课程",
                    "recommendation": recommendation
                })
    
    # 清空历史按钮
    if st.button("清空对话历史"):
        st.session_state.chat_history = []
        st.rerun() 